<?php

namespace app\common\model;

use app\common\util\ErrorCode;
use app\common\util\ThirdTool;
use app\common\util\Tools;
use think\facade\Log;
use think\Model;

/**
 * Class Auth
 * @package app\common\model
 * 用户登录表
 */
class trUserToken extends Model
{
    protected $pk = 'id';
    protected $resultSetType = 'collection'; //申明结果集是数据集对象（collection），查询结果才能用toArray()方法转为数组
    protected $name = "user_token"; //表名
    static $cacheTime = 86400;

//    protected $type = [
//        'expire_time'  =>  'timestamp:Y-m-d H:i',
//    ];

    //获取多字段数据
    public static function getFields($field, $filter = [], $one = true, $toArray = true){
        if($one){
            $data = self::where($filter)->field($field)->find();
            return $data ? ($toArray ? Tools::transferFields($data->toArray()) : Tools::transferFields($data->getData())) : [];
        }else{
            $data = self::where($filter)->field($field)->select();
            return $data ? ($toArray ? Tools::transferFields($data->toArray()) : $data) : [];
        }
    }

    //根据条件获取数量
    public static function getCount($filter){
        if(!empty($filter)){
            return self::where($filter)->count();
        }else{
            return self::count();
        }
    }

    //添加
    public static function add($data, $getId=false){
        if(!$data)
            return true;
        try {
            if ($getId) {
                $data["mall_id"] = request()->mallData["id"];
                $model = self::create($data);
                return $model->id;
            } else {
                return self::create($data);
            }
        }catch (\Exception $e){
            return ErrorCode::code("try_error", $e->getMessage());
        }
    }

    //更新
    public static function doUpdate($data, $filter = []){
        if(!$data)
            return true;
        return self::update($data, $filter);
    }

    //获取某字段的值
    public static function getValue($field, $filter = []){
        if(!is_array($field)){
            return self::where($filter)->value($field);
        }
        return false;
    }

    //获取某字段的所有值
    public static function getColumn($field, $filter = [], $key = ''){
        if(!is_array($field)){
            return self::where($filter)->column($field, $key);
        }
        return [];
    }

    //根据删除数据
    public static function delByFilter($where){
        $result = self::where($where)->delete();
        return $result;
    }

    //根据ID删除数据
    public static function delByUserId($id){
        if(is_array($id)){
            $token = self::getColumn("token", [["user_id", "in", $id]]);
            $result = self::where('user_id', 'in', $id)->delete();
        }else{
            $token = self::getColumn("token", [["user_id", "=", $id]]);
            $result = self::where('user_id', $id)->delete();
        }
        if($token){
            foreach ($token as $i){
                cache("web_" . $i, null); //登录信息放缓存
            }
        }
        return $result;
    }
}